#!/bin/bash
LOG="data.txt"
OUTDIR="results"
mkdir -p "$OUTDIR"

echo "基本统计：" > "$OUTDIR/summary.txt"
echo "行数: $(wc -l < $LOG)" >> "$OUTDIR/summary.txt"
echo "单词数: $(wc -w < $LOG)" >> "$OUTDIR/summary.txt"
echo "字符数: $(wc -c < $LOG)" >> "$OUTDIR/summary.txt"

echo -e "\n日志级别统计：" >> "$OUTDIR/summary.txt"
awk '{print $3}' $LOG | sort | uniq -c | sort -nr >> "$OUTDIR/summary.txt"

echo -e "\n用户统计：" >> "$OUTDIR/summary.txt"
awk '{print $4}' $LOG | sort | uniq -c | sort -nr >> "$OUTDIR/summary.txt"

echo -e "\nTop 错误消息：" >> "$OUTDIR/summary.txt"
grep "ERROR" $LOG | awk '{$1=$2=$3=$4=""; sub(/^ +/,""); print}' | sort | uniq -c | sort -nr | head -n 5 >> "$OUTDIR/summary.txt"
